<?php
namespace App\Services;
use App\Repositories\TestingDetailRepository;
use App\Services\Algo\CustomerQuestionnaireTestingAlgo;
use App\Services\Algo\DefaultAlgo;
use App\Services\Algo\FiveDimensionTestingAlgo;
use App\Services\Algo\HealthTestingAlgo;
use App\Services\Algo\HighBloodPressureAlgo;
use App\Services\Algo\PsyTestingAlgo;
use App\Services\Algo\DecreaseWeightAlgo;
use App\Services\Algo\RecipeTestingAlgo;
use App\Services\Algo\TbTestingAlgo;
use App\Services\Algo\TbWestMediAlgo;
use App\Services\Algo\OilTestAlgo;
use App\Services\Algo\FourteenDecWeightAlgo;
use App\Services\Algo\DWeightNoqrCodeAlgo;
use App\Services\Algo\ZAhealthAlgo;
use App\Services\Algo\FigureAlgo;
use App\Services\Algo\PostpartumDepressionAlgo;
use App\Services\Algo\SomnolenceDuringPregnancyTestingAlgo;
use App\Services\Algo\BoneHealthDuringPregnancyTestingAlgo;
use App\Services\Algo\PostpartumWeightRetentionTestingAlgo;
use App\Services\Algo\ChildhoodAutismAlgo;
use App\Services\Algo\MentalHealthTestingAlgo;
use App\Services\Algo\JointDexterityTestingAlgo;
use App\Services\Algo\PressureTestingAlgo;
use App\Services\Algo\ChineseMedicineConstitutionTestingHlsAlgo;
use App\Services\Algo\ZAhealthVtwoTestingAlgo;
use App\Services\Algo\InfantBehavioralNerveTestingAlgo;
use App\Services\Algo\QuestionnaireTestingAlgo;
use App\Services\Algo\ZYhealthAlgo;
use App\Services\Algo\SleepingTestingNewAlgo;
use App\Services\Algo\ExerciseLevelTestingAlgo;
use App\Services\Algo\ZADietAlgo;
use App\Services\Algo\ZAPressureAlgo;
use App\Services\Algo\ZAMentalAlgo;
use App\Services\Algo\ElbooTestingAlgo;
use App\Services\Algo\MetaWeightAlgo;
use App\Models\LogHelper;
use TestingConfig;
use ParamConfig;
use App\Services\Algo\EnterpriceHealthTestingAlgo;
use TestingDetailService;
/**
 * php AES加解密类
 * 如果要与java共用，则密钥长度应该为16位长度
 * 因为java只支持128位加密，所以php也用128位加密，可以与java互转。
 * 同时AES的标准也是128位。只是RIJNDAEL算法可以支持128，192和256位加密。
 * java 要使用AES/CBC/NoPadding标准来加解密
 *
 * @author Terry
 *
 */
class TestingAlgoHelper
{

    /**
     * 计算分数的接口方法
     * @param $tid
     * @param $answerStr
     * @param $typeFourExpress
     * @return array
     */
    public function calculate($tid, $answerStr,$typeFourExpress){
        $testingDetail = TestingDetailService::getById($tid);
        //这部分为通过减脂后台自动创建问卷的逻辑算法部分
        if(isset($testingDetail['create_by']) && $testingDetail['create_by'] == TestingDetailRepository::CREATE_BY_JZ_CUSTOMER){
            $instance = new CustomerQuestionnaireTestingAlgo($tid,$answerStr,$typeFourExpress);
        }else{
            //需要自定义增加的测评算法部分请放在这里
            LogHelper::log(__CLASS__.'::'.__FUNCTION__.'::data');
            LogHelper::log('typeFourExpress===========>');
            LogHelper::log($typeFourExpress);
            $result = [];
            $tbTradionTid = intval(TestingConfig::getValue('TB.testing.tradition.id',22));
            $tbWestTid = intval(TestingConfig::getValue('TB.testing.west.id',23));
            $recipeTestingTid = intval(TestingConfig::getValue('RT.testing.id',24));
            $oilTestTid = intval(TestingConfig::getValue('Oil.testing.id',25));
            //14天減重
            $fourteendw = intval(TestingConfig::getValue('TW.fat.testing.id',19));
            //21天减重无二维码
            $dwnoqrcode = intval(TestingConfig::getValue('DW.noqrcode.fat.testing.id',18));
            //众安健康测评
            $zaHealthId = intval(TestingConfig::getValue('ZA.health.id',33));
            //众安健康测评2.0
            $zaHealthIdVtwo = intval(TestingConfig::getValue('ZA.V2.health.id',43));
            //s14轻体测问卷
            $questionnaireTid = intval(TestingConfig::getValue('s14_questionnaire.id',53));
            //s14轻体测结束问卷
            $questionnaireOverTid = intval(TestingConfig::getValue('s14_over_questionnaire.id',54));
            //s21轻体测第一周问卷
            $questionnaireOneTid = intval(TestingConfig::getValue('s21_one_questionnaire.id',55));
            //s21轻体测第二周问卷
            $questionnaireTwoTid = intval(TestingConfig::getValue('s21_two_questionnaire.id',56));
            //s21轻体测结束问卷
            $questionnaireOversTid = intval(TestingConfig::getValue('s21_over_questionnaire.id',57));
            //JHR健康问卷
            $jhrTid =  intval(TestingConfig::getValue('jhr_testing.id',59));
            //YC轻体调查问卷
            $ycTid =  intval(TestingConfig::getValue('yc_testing.id',60));
            //减脂后台引流测评
            $flowTid =  intval(TestingConfig::getValue('flow_testing.id',67));
            //减脂后台引流测评（hw测评）
            $flowJhrTid =  intval(TestingConfig::getValue('jhr_testing_special.id',68));
            //益步14天轻体测评
            $elbooTid =  intval(TestingConfig::getValue('elboo.testing.id',73));
            //益步7天轻体测评
            $elbooSevenTid =  intval(TestingConfig::getValue('elboo.seven.testing.id',74));
            //益步7天问卷
            $questionnaireS7Tid = intval(TestingConfig::getValue('s7_questionnaire.id',82));
            //茄子健康7天轻体测评
            $sevenTid =  intval(TestingConfig::getValue('qiezi.s7.testing.id',74));
            //茄子健康通用轻体测评
            $currencyTid =  intval(TestingConfig::getValue('qiezi.currency.testing.id',84));
            //代谢用户测评
            $metaTid = intval(TestingConfig::getValue('jz.meta.testing.id',86));
            //s3结束问卷
            $thereQuestionnaireTid = intval(TestingConfig::getValue('s3_over_questionnaire.id',87));
            //代谢用户测评(题目优化版)
            $metaOptTid = intval(TestingConfig::getValue('jz.meta.testing.opt.id',90));

            $figureTestTid = intval(TestingConfig::getValue('Figure.testing.id',31));
            $PostpartumDepressionTestTid = intval(TestingConfig::getValue('postpartum.depression.testing.id',32));
            $SomnolenceDuringPregnancyTestTid = intval(TestingConfig::getValue('somnolence.during.pregnancy.testing.id',34));
            $BoneHealthDuringPregnancyTestTid = intval(TestingConfig::getValue('bone.health.during.pregnancy.testing.id',35));
            $PostpartumWeightRetentionTestTid = intval(TestingConfig::getValue('postpartum.weight.retention.testing.id',36));
            $ChildhoodAutismTestingTid = intval(TestingConfig::getValue('childhood.autism.testing.id',37));

            $PressureTestingTid = intval(TestingConfig::getValue('pressure.testing.id',38));
            $MentalHealthTestingTid = intval(TestingConfig::getValue('mental_health_testing.id',39));
            $JointDexterityTestingTid = intval(TestingConfig::getValue('joint.dexterity.testing.id',40));

            $InfantBehavioralNerveTestingTid = intval(TestingConfig::getValue('infant_behavioral_nerve_testing.id',41));
            $ChineseMedicineConstitutionTestingHlsTid = intval(TestingConfig::getValue('chinese_medicine_constitution_testing_hls.id',42));
            $ZyReplaceTestingTid = intval(TestingConfig::getValue('zy_replace_testing.id',58));
            $SleepingTestingNewTid = intval(TestingConfig::getValue('sleeping_testing_new.id',65));
            $ExerciseLevelTestingTid = intval(TestingConfig::getValue('exercise_level_testing.id',66));

            $zaDietTid = intval(TestingConfig::getValue('za.diet.testing.id', 69));
            $zaPressureTid = intval(TestingConfig::getValue('za.pressure.testing.id', 70));
            $zaMentalTid = intval(TestingConfig::getValue('za.mental.testing.id', 71));
            $enterpriceHealthTestingTid = intval(TestingConfig::getValue('enterprice.health.testing.id', 79));
            // 国寿测评
            $gsTid = intval(TestingConfig::getValue('gs.testing.id', 78));

            //五维测评免费版
            $fiveDimension = intval(TestingConfig::getValue('five.dimension.testing.id', 92));

            //五维测评付费版
            $fiveDimensionPay = intval(TestingConfig::getValue('five.dimension.testing.pay.id', 93));

            LogHelper::log('id===========>');
            $logData = [
                'tid' => $tid,
                'answerStr' => $answerStr,
                'recipeTestingTid' => $recipeTestingTid,
            ];
            LogHelper::log($logData);
            switch($tid){
                case 1 :
                    $instance = new HighBloodPressureAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case 2 :
                    $instance = new HealthTestingAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case 3 :
                    $instance = new PsyTestingAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case 21 :
                    $instance = new DecreaseWeightAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $tbTradionTid :
                    $instance = new TbTestingAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $tbWestTid :
                    $instance = new TbWestMediAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $oilTestTid :
                    $instance = new OilTestAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $recipeTestingTid :
                    $instance = new RecipeTestingAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $fourteendw :
                    $instance = new FourteenDecWeightAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $dwnoqrcode:
                    $instance = new DWeightNoqrCodeAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $zaHealthId:
                case $zaHealthIdVtwo:
                case $gsTid:
                    $instance = new ZAhealthAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $figureTestTid :
                    $instance = new FigureAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $PostpartumDepressionTestTid :
                    $instance = new PostpartumDepressionAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $SomnolenceDuringPregnancyTestTid :
                    $instance = new SomnolenceDuringPregnancyTestingAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $BoneHealthDuringPregnancyTestTid :
                    $instance = new BoneHealthDuringPregnancyTestingAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $PostpartumWeightRetentionTestTid :
                    $instance = new PostpartumWeightRetentionTestingAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $ChildhoodAutismTestingTid :
                    $instance = new ChildhoodAutismAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $PressureTestingTid :
                    $instance = new PressureTestingAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $MentalHealthTestingTid :
                    $instance = new MentalHealthTestingAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $JointDexterityTestingTid :
                    $instance = new JointDexterityTestingAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $InfantBehavioralNerveTestingTid :
                    $instance = new InfantBehavioralNerveTestingAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $ChineseMedicineConstitutionTestingHlsTid :
                    $instance = new ChineseMedicineConstitutionTestingHlsAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $questionnaireTid:
                    $instance = new QuestionnaireTestingAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $questionnaireOverTid:
                    $instance = new QuestionnaireTestingAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $questionnaireOneTid:
                    $instance = new QuestionnaireTestingAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $questionnaireTwoTid:
                    $instance = new QuestionnaireTestingAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $questionnaireOversTid:
                    $instance = new QuestionnaireTestingAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $ZyReplaceTestingTid:
                    $instance = new ZYhealthAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $jhrTid:
                    $instance = new QuestionnaireTestingAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $ycTid:
                    $instance = new QuestionnaireTestingAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $flowTid:
                    $instance = new DecreaseWeightAlgo(21,$answerStr,$typeFourExpress);
                    break;
                case $flowJhrTid:
                    $instance = new QuestionnaireTestingAlgo(59,$answerStr,$typeFourExpress);
                    break;
                case $SleepingTestingNewTid:
                    $instance = new SleepingTestingNewAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $ExerciseLevelTestingTid:
                    $instance = new ExerciseLevelTestingAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $zaDietTid:
                    $instance = new ZADietAlgo($tid ,$answerStr, $typeFourExpress);
                    break;
                case $zaPressureTid:
                    $instance = new ZAPressureAlgo($tid ,$answerStr, $typeFourExpress);
                    break;
                case $zaMentalTid:
                    $instance = new ZAMentalAlgo($tid ,$answerStr, $typeFourExpress);
                    break;
                case $elbooTid:
                    $instance = new ElbooTestingAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $elbooSevenTid:
                    $instance = new ElbooTestingAlgo($elbooTid,$answerStr,$typeFourExpress);
                    break;
                case $sevenTid:
                    $instance = new FourteenDecWeightAlgo(19,$answerStr,$typeFourExpress);
                    break;
                case $currencyTid:
                    $instance = new FourteenDecWeightAlgo(19,$answerStr,$typeFourExpress);
                    break;
                case $enterpriceHealthTestingTid:
                    $instance = new EnterpriceHealthTestingAlgo($tid, $answerStr, $typeFourExpress);
                    break;
                case $questionnaireS7Tid:
                    $instance = new QuestionnaireTestingAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $metaTid :
                    $instance = new MetaWeightAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $metaOptTid :
                    $instance = new MetaWeightAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $thereQuestionnaireTid:
                    $instance = new QuestionnaireTestingAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                case $fiveDimension:
                case $fiveDimensionPay:
                    $instance = new FiveDimensionTestingAlgo($tid,$answerStr,$typeFourExpress);
                    break;
                default :
                    $instance = new DefaultAlgo($tid,$answerStr,$typeFourExpress);
                    break;
            }
        }
        $result['totalScore'] = $instance->calculate();
        $result['finalAnswerStr'] = $instance->resultToStr();

        return $result;
    }

    /**测评结论中额外参数
     * @param $tid
     * @param $answerStr
     * @return mixed|null
     */
    protected function getParame($tid, $answerStr,$typeFourExpress){
        $extra = null;
        switch($tid){
            case 1:
                $instance = new HighBloodPressureAlgo($tid,$answerStr,$typeFourExpress);
                $extra = $instance->getExtraParam();
                break;
        }
        return $extra;
    }


    /**返回测评结论
     * @param $tid
     * @param $score
     * @param $answerStr
     * @param $title
     * @return string
     */
    public function getConclusion($tid,$score,$answerStr,$title,$typeFourExpress){

        $extra = $this -> getParame($tid, $answerStr,$typeFourExpress);
        $title_str= '';

        $scoreTitleKey = 'testing.score.title.'.$tid;
        $scoreTitle = strip_tags(ParamConfig::getValue($scoreTitleKey,'结果分析',true));

        $recipeTestingTid = intval(TestingConfig::getValue('RT.testing.id',24));
        $figureTestingTid = intval(TestingConfig::getValue('figure.testing.id',31));
        $PostpartumDepressionTestingTid = intval(TestingConfig::getValue('postpartum.depression.testing.id',32));
        $SomnolenceDuringPregnancyTestingTid = intval(TestingConfig::getValue('somnolence.during.pregnancy.testing.id',34));
        $BoneHealthDuringPregnancyTestingTid = intval(TestingConfig::getValue('bone.health.during.pregnancy.testing.id',35));
        $PostpartumWeightRetentionTestingTid = intval(TestingConfig::getValue('postpartum.weight.retention.testing.id',36));

        // 新的众安健康测评
        $zaTestTid = intval(TestingConfig::getValue('ZA.V2.health.id', 43));

        switch($tid){
            case 1:
                $title_str="您在未来".$extra."年的高血压风险概率为".$score."%";
                break;
            case 2:
                $title_str="您目前的综合健康评分为:".$score.",".$title;
                break;
            case 3:
                $title_str="您目前的心理压力分值为:".$score.",".$title;
                break;
            case $recipeTestingTid:
//                $title_str="您的健康食谱标签如下";
                $title_str="";
                break;
            case $figureTestingTid:
                $title_str='';
                 break;
            case $PostpartumDepressionTestingTid:
                $title_str='您的产后抑郁评分为'.$score.'，'.$title;
                break;
            case $SomnolenceDuringPregnancyTestingTid:
                $title_str='您的孕期嗜睡指数为'.$score.'，'.$title;
                break;
            case $BoneHealthDuringPregnancyTestingTid:
                $title_str='您的准妈妈骨骼健康测评分数为'.$score.'，'.$title;
                break;
            case $PostpartumWeightRetentionTestingTid:
                $title_str='';
                break;
            default:
                $scoreTitle = str_replace('{{SCORE}}',$score,$scoreTitle);
                $scoreTitle = str_replace('{{DESCRIPTION}}',$title,$scoreTitle);
                $title_str=$scoreTitle;
        }

        return $title_str;
    }

}
